package logic

import (
	"anban/models"
	"anban/service"
	"anban/utils"
	"anban/utils/wechat"
	"fmt"
	"github.com/astaxie/beego"
	"strings"
	"time"
)

// 检查学生是否按时到校
func CheckStudentGoSchool(school *models.School) {
	if len(school.MorningTime) < 1 {
		return
	}

	var p map[string]interface{}
	tplId := beego.AppConfig.String("goschoollatetplid")

	morningTimeArr := strings.Split(school.MorningTime, " - ")
	if len(morningTimeArr) < 1 {
		return
	}

	morningTimeStr := fmt.Sprintf("%s %s", time.Now().Format("2006-01-02"), morningTimeArr[0])
	loc, _ := time.LoadLocation("Local")
	morningTime, _ := time.ParseInLocation("2006-01-02 15:04:05", morningTimeStr, loc)
	intervalSeconds := time.Now().Unix() - morningTime.Unix()

	if intervalSeconds >= 600 && intervalSeconds < 700 {
		p = map[string]interface{}{}
		p["schoolId"] = school.Id
		p["status"] = 1
		_, studentList := service.GetStudentList(p)

		for _, student := range studentList {
			if student.Id == 14 && student.IsAtSchool != 1 && len(student.FamilySn) > 0 && school.IsUseMobileMsg == 1 {
				p = map[string]interface{}{}
				p["familySn"] = student.FamilySn
				p["status"] = 1
				_, parentList := service.GetUserList(p)

				for _, parent := range parentList {
					if parent.SubscribeStatus == 1 {
						wechat.SendSafeNotice(parent.OpenId, "您孩子没有按时到校", "", "", student.RealName, time.Now().Format("2006-01-02 15:04:05"), "未按时到校")
					}
					if len(parent.Phone) > 1 {
						utils.SendMobileMessage([]string{parent.Phone}, tplId, []string{parent.RealName})
					}
				}
			}
		}
	}

	return
}

// 检查学生是否按时离校
func CheckStudentLeaveSchool(school *models.School) {
	if len(school.AfternoonTime) < 1 {
		return
	}

	var p map[string]interface{}
	tplId := beego.AppConfig.String("leaveschoollatetplid")

	afternoonTimeArr := strings.Split(school.AfternoonTime, " - ")
	if len(afternoonTimeArr) < 2 {
		return
	}

	afternoonTimeStr := fmt.Sprintf("%s %s", time.Now().Format("2006-01-02"), afternoonTimeArr[1])
	loc, _ := time.LoadLocation("Local")
	afternoonTime, _ := time.ParseInLocation("2006-01-02 15:04:05", afternoonTimeStr, loc)
	intervalSeconds := time.Now().Unix() - afternoonTime.Unix()

	if intervalSeconds >= 600 && intervalSeconds < 700 {
		p = map[string]interface{}{}
		p["schoolId"] = school.Id
		p["status"] = 1
		_, studentList := service.GetStudentList(p)

		for _, student := range studentList {
			if student.Id == 14 && student.IsAtSchool != 0 && len(student.FamilySn) > 0 && school.IsUseMobileMsg == 1 {
				p = map[string]interface{}{}
				p["familySn"] = student.FamilySn
				p["status"] = 1
				_, parentList := service.GetUserList(p)

				for _, parent := range parentList {
					if parent.SubscribeStatus == 1 {
						wechat.SendSafeNotice(parent.OpenId, "您孩子没有按时离校", "", "", student.RealName, time.Now().Format("2006-01-02 15:04:05"), "未按时离校")
					}
					if len(parent.Phone) > 1 {
						utils.SendMobileMessage([]string{parent.Phone}, tplId, []string{parent.RealName})
					}
				}
			}
		}
	}

	return
}

// 发送门禁刷卡微信通知
func SendAccessControlWechatNotice(student *models.Student, status string, remark string) {
	domain := beego.AppConfig.String("domain")
	path := beego.URLFor("parent.SafeNoticeController.ShowList")
	wechatUrl := fmt.Sprintf("%s%s", domain, path)
	familySn := student.FamilySn

	if len(student.FamilySn) > 0 {
		p := map[string]interface{}{}
		p["familySn"] = familySn
		p["status"] = 1
		p["subscribeStatus"] = 1
		_, userList := service.GetUserList(p)
		for _, user := range userList {
			wechat.SendSafeNotice(user.OpenId, "您有新的校园门禁通知", remark, wechatUrl, student.RealName, time.Now().Format("2006-01-02 15:04:05"), status)
		}
	}

	return
}